package com.zxl.openapi.listener;

import com.zxl.openapi.feign.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import java.util.Map;
import java.util.Set;

import static com.zxl.openapi.constans.SystemParameter.*;

/**
 * 八大监听器 ServletContextListener,ServletContextAttributeListener, HttpSessionListener, HttpSessionBindingListener, HttpSessionAttributeListener, HttpSessionActivationListener, ServletRequestAttributeListener,ServletContextListener
 */
@Component
public class MyContextLoadListener implements ServletContextListener {

    Logger logger = LoggerFactory.getLogger(MyContextLoadListener.class);

    @Autowired
    private RedisService redisService;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            //在这里加载数据 ->链接redis 查询数据
            Set<String> keys = redisService.keys(REDIS_APIPRE + "*");

            allApiInfoMap.clear();

            for (String key : keys) {

                Map<Object, Object> result = redisService.hgetAll(key);

                allApiInfoMap.put(key,result);



            }
            System.out.println(allApiInfoMap);
            logger.error("初始化api信息，得到的结果是：{}",allApiInfoMap);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
